import { ref } from 'vue';
import type { Banner, Food, Merchant, Promotion } from '../types';
import { getMerchantsByRegion } from '@/api/merchant';

export function useHomeData() {
  // 通知数据
  const notices = ref<string[]>([
    '🎉 新功能上线：愿望榜单，说出你想吃的美食！',
    '🔥 热门活动：满50减15，新客立减8元',
    '📢 系统升级公告：今晚22:00-23:00进行系统维护'
  ]);

  // 轮播图数据
  const banners = ref<Banner[]>([
    {
      id: 1,
      image: '/static/food01.png',
      type: 'merchant',
      targetId: 1,
      productId: undefined
    },
    {
      id: 2,
      image: '/static/food02.png',
      type: 'food',
      targetId: 1,
      productId: 1
    },
    {
      id: 3,
      image: '/static/food03.png',
      type: 'merchant',
      targetId: 1,
      productId: undefined
    }
  ]);

  // 今日优选美食
  const selectedFoods = ref<Food[]>([
    {
      id: 1,
      name: '麻辣小面套餐',
      image: '/static/food01.png',
      price: 23.0,
      sales: 256,
      merchantId: 1,
      productId: 1
    },
    {
      id: 2,
      name: '红烧牛肉面',
      image: '/static/food02.png',
      price: 28.0,
      sales: 189,
      merchantId: 1,
      productId: 2
    },
    {
      id: 3,
      name: '酸辣粉',
      image: '/static/food03.png',
      price: 25.0,
      sales: 342,
      merchantId: 1,
      productId: 3
    },
    {
      id: 4,
      name: '重庆小面',
      image: '/static/food01.png',
      price: 48.0,
      sales: 167,
      merchantId: 1,
      productId: 4
    }
  ]);

  // 合作商家数据
  const merchants = ref<Merchant[]>([
    {
      id: 1,
      name: '超级麻辣小面馆',
      logo: '/static/shangjia01.png',
      coverImage: '/static/food01.png',
      rating: 4.8,
      sales: 2345,
      distance: 1.2,
      minPrice: 20,
      deliveryFee: 3,
      tags: ['川菜', '快餐', '面食']
    },
    {
      id: 2,
      name: '和风寿司料理',
      logo: '/static/shangjia02.png',
      coverImage: '/static/food02.png',
      rating: 4.6,
      sales: 1876,
      distance: 2.5,
      minPrice: 30,
      deliveryFee: 4,
      tags: ['寿司', '日料']
    },
    {
      id: 3,
      name: '川味小馆',
      logo: '/static/shangjia03.png',
      coverImage: '/static/food03.png',
      rating: 4.5,
      sales: 3201,
      distance: 0.8,
      minPrice: 15,
      deliveryFee: 2,
      tags: ['川菜', '快餐']
    }
  ]);

  // 优惠活动数据
  const promotions = ref<Promotion[]>([
    {
      id: 1,
      image: '/static/shangjia01.png',
      tag: '满减',
      title: '满50减15元',
      desc: '绿光餐厅全场通用',
      endTime: '2天',
      merchantId: 1
    },
    {
      id: 2,
      image: '/static/shangjia02.png',
      tag: '新客',
      title: '新客立减8元',
      desc: '和风寿司新客专享',
      endTime: '5天',
      merchantId: 2
    },
    {
      id: 3,
      image: '/static/shangjia03.png',
      tag: '折扣',
      title: '全场8折',
      desc: '川味小馆限时优惠',
      endTime: '3天',
      merchantId: 3
    }
  ]);

  // ⭐ 加载附近商家（对接后端）
  const loadNearbyMerchants = async (regionId: number) => {
    try {
      const data = await getMerchantsByRegion(regionId.toString());
      
      if (Array.isArray(data)) {
        merchants.value = data.map((m: any) => ({
          id: m.merchantId,
          name: m.merchantName || m.name,
          logo: m.avatarUrl || '/static/shangjia01.png',
          coverImage: m.avatarUrl || '/static/food01.png',
          rating: m.rating || 4.5,
          sales: m.salesCount || 0,
          distance: 1.2, // 需要根据经纬度计算
          minPrice: m.minOrderAmount || 20,
          deliveryFee: m.deliveryFee || 3,
          tags: m.tags ? m.tags.split(',') : []
        }));
      }
    } catch (error) {
      console.error('加载附近商家失败:', error);
    }
  };

  return {
    notices,
    banners,
    selectedFoods,
    merchants,
    promotions,
    loadNearbyMerchants
  };
}